package com.ideal.view.form;

import com.ideal.core.builder.ClassBuilder;
import com.ideal.core.constant.Type;
import com.ideal.utils.Empty;
import com.ideal.utils.StringUtils;
import com.ideal.view.settings.TemplateBaseInfo;
import com.ideal.view.settings.TemplateSetting;
import java.io.File;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.HashMap;
import java.util.Map;

public class TableConfigForm {
    private JPanel rootPanel;
    private JTextField tableName;
    private JCheckBox Controller;
    private JCheckBox IService;
    private JCheckBox ServiceImpl;
    private JCheckBox IDao;
    private JCheckBox DaoImpl;
    private JCheckBox Domain;
    private JCheckBox Domain_hbm_xml;
    private JButton generate;
    private JTextField moduleName;
    private JCheckBox form;
    private String path;

    private Map<Type, JCheckBox> all = new HashMap<>();
    private Map<Type, JCheckBox> choose = new HashMap<>();

    public JPanel getRootPanel() {
        return rootPanel;
    }

    public TemplateBaseInfo info = TemplateSetting.getInstance().getInfo();
    public TableConfigForm(String path) {
        this.path = path;
        build();
        generate.addActionListener((ActionEvent e) -> {
            generate(this.path);
        });
    }

    /**
     * 生成代码
     *
     * @param path
     */
    public void generate(String path) {
        Toolkit.getDefaultToolkit().beep();
        // 从设置面板获得的路径
        String templatePath = "";
        // 从设置面板获得路径得到的文件对象
        File file = null;
        // 从设置面板获得文件的文件名
        String fileName = "";
        // 从设置面板获得的文件的文件夹路径
        String dirName = "";
        // 生成文件的保存路径
        path = path+"/";
        for (Type type : all.keySet()) {
            if (all.get(type).isSelected()) {
                choose.put(type, all.get(type));
            }
        }
        // 表名不可以为空
        if (tableName.getText() != null && "".equals(tableName.getText().trim())) {
            JOptionPane.showMessageDialog(null, "请输入表名");
        }
        // 模块中文名不可以为空
        else if(moduleName.getText()!=null&&"".equals(moduleName.getText().trim())){
            JOptionPane.showMessageDialog(null, "请输入模块中文名，用于代码注释！");
        }
        //进行处理
        else {
            String table = tableName.getText();
            String module = moduleName.getText();
            try {
                for (Type type : choose.keySet()) {
                    switch (type) {
                        case CONTROLLER:
                            templatePath = info.getController();
                            if(!Empty.isEmpty(templatePath)){
                                file = new File(templatePath);
                                fileName = file.getName();
                                dirName = StringUtils.getDirPath(templatePath,fileName);
                                ClassBuilder.generateJava(Type.CONTROLLER, fileName, path,table,module,dirName);
                            }else{
                                ClassBuilder.generateJava(Type.CONTROLLER, "controller.ftl", path,table,module,null);
                            }
                            break;
                        case SERVICE:
                            templatePath = info.getService();
                            if(!Empty.isEmpty(templatePath)){
                                file = new File(templatePath);
                                fileName = file.getName();
                                dirName = StringUtils.getDirPath(templatePath,fileName);
                                ClassBuilder.generateJava(Type.SERVICE, fileName, path,table,module,dirName);
                            }else{
                                ClassBuilder.generateJava(Type.SERVICE, "service.ftl", path,table,module,null);
                            }
                            break;
                        case SERVICE_IMPL:
                            templatePath = info.getServiceImpl();
                            if(!Empty.isEmpty(templatePath)){
                                file = new File(templatePath);
                                fileName = file.getName();
                                dirName = StringUtils.getDirPath(templatePath,fileName);
                                ClassBuilder.generateJava(Type.SERVICE_IMPL, fileName, path,table,module,dirName);
                            }else{
                                ClassBuilder.generateJava(Type.SERVICE_IMPL, "serviceImpl.ftl", path,table,module,null);
                            }
                            break;
                        case DAO:
                            templatePath = info.getDao();
                            if(!Empty.isEmpty(templatePath)){
                                file = new File(templatePath);
                                fileName = file.getName();
                                dirName = StringUtils.getDirPath(templatePath,fileName);
                                ClassBuilder.generateJava(Type.DAO, fileName, path,table,module,dirName);
                            }else{
                                ClassBuilder.generateJava(Type.DAO, "dao.ftl", path,table,module,null);
                            }
                            break;
                        case DAO_IMPL:
                            templatePath = info.getDaoImpl();
                            if(!Empty.isEmpty(templatePath)){
                                file = new File(templatePath);
                                fileName = file.getName();
                                dirName = StringUtils.getDirPath(templatePath,fileName);
                                ClassBuilder.generateJava(Type.DAO_IMPL, fileName, path,table,module,dirName);
                            }else{
                                ClassBuilder.generateJava(Type.DAO_IMPL, "daoImpl.ftl", path,table,module,null);
                            }
                            break;
                        case DOMAIN:
                            templatePath = info.getDomain();
                            if(!Empty.isEmpty(templatePath)){
                                file = new File(templatePath);
                                fileName = file.getName();
                                dirName = StringUtils.getDirPath(templatePath,fileName);
                                ClassBuilder.generateJava(Type.DOMAIN, fileName, path,table,module,dirName);
                            }else{
                                ClassBuilder.generateJava(Type.DOMAIN, "domain.ftl", path,table,module,null);
                            }
                            break;
                        case DOMAIN_MAPPING:
                            templatePath = info.getDomainMapping();
                            if(!Empty.isEmpty(templatePath)){
                                file = new File(templatePath);
                                fileName = file.getName();
                                dirName = StringUtils.getDirPath(templatePath,fileName);
                                ClassBuilder.generateJava(Type.DOMAIN_MAPPING, fileName, path,table,module,dirName);
                            }else{
                                ClassBuilder.generateJava(Type.DOMAIN_MAPPING, "domain_htm_xml.ftl", path,table,module,null);
                            }
                            break;
                        case FORM:
                            templatePath = info.getForm();
                            if(!Empty.isEmpty(templatePath)){
                                file = new File(templatePath);
                                fileName = file.getName();
                                dirName = StringUtils.getDirPath(templatePath,fileName);
                                ClassBuilder.generateJsp(Type.FORM, fileName, path,table,module,dirName);
                            }else{
                                ClassBuilder.generateJsp(Type.FORM, "form.ftl", path,table,module,null);
                            }
                            break;
                        default:
                            break;
                    }
                }
                JOptionPane.showMessageDialog(null, "代码生成成功！");
            }catch (Exception e){
                JOptionPane.showMessageDialog(null, "程序异常!"+e.getMessage());
            }
        }
    }


    /**
     * 将JCheckbox放入map
     */
    private void build() {
        all.put(Type.CONTROLLER, Controller);
        all.put(Type.SERVICE, IService);
        all.put(Type.SERVICE_IMPL, ServiceImpl);
        all.put(Type.DAO, IDao);
        all.put(Type.DAO_IMPL, DaoImpl);
        all.put(Type.DOMAIN, Domain);
        all.put(Type.DOMAIN_MAPPING, Domain_hbm_xml);
        all.put(Type.FORM,form);
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }
}

